package com.wjy.common.util.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @author ybxxszl
 * @desc RedisTemplate工具类
 * @date 2019/4/15
 */
@Component
public class RedisTemplateUtil {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /*
     * common
     */

    /**
     * 根据key判断缓存是否存在
     *
     * @param key
     * @return
     */
    public boolean hasKey(Object key) {
        try {
            Boolean b = redisTemplate.hasKey(key);
            return b;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据keys删除缓存
     *
     * @param keys
     * @return
     */
    public long delete(Object... keys) {
        try {
            if (keys.length > 1) {
                Long l = redisTemplate.delete(Arrays.asList(keys));
                return l;
            } else if (keys.length == 1) {
                Boolean b = redisTemplate.delete(keys[0]);
                if (b) {
                    return 1;
                }
            }
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 设置缓存失效时间
     *
     * @param key
     * @param timeout
     * @return
     */
    public boolean expire(Object key, long timeout) {
        try {
            if (timeout > 0) {
                Boolean b = redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
                return b;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据key获取缓存失效时间
     *
     * @param key
     * @return
     */
    public long getExpire(Object key) {
        try {
            Long l = redisTemplate.getExpire(key, TimeUnit.SECONDS);
            return l;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /*
     * string
     */

    /**
     * 放入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean setValue(Object key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 放入缓存并且设置失效时间
     *
     * @param key
     * @param value
     * @param timeout
     * @return
     */
    public boolean setValue(Object key, Object value, long timeout) {
        try {
            if (timeout > 0) {
                redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据key获取缓存
     *
     * @param key
     * @return
     */
    public Object getValue(Object key) {
        try {
            Object value = redisTemplate.opsForValue().get(key);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据key增加或减少缓存的值
     *
     * @param key
     * @param delta
     * @param change
     * @return
     */
    public long changeValue(Object key, long delta, boolean change) {
        try {
            Long l;
            if (change) {
                l = redisTemplate.opsForValue().increment(key, delta);
            } else {
                l = redisTemplate.opsForValue().decrement(key, delta);
            }
            return l;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /*
     * list
     */

    /**
     * 推送缓存
     *
     * @param key
     * @param value
     * @return
     */
    public long leftPushList(Object key, Object value) {
        try {
            Long l = redisTemplate.opsForList().leftPush(key, value);
            return l;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 弹出缓存
     *
     * @param key
     * @return
     */
    public Object rightPopList(Object key) {
        try {
            Object value = redisTemplate.opsForList().rightPop(key);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 添加缓存
     *
     * @param key
     * @param member
     * @param score
     * @return
     */
    public boolean addZSet(Object key, Object member, double score) {
        try {
            Boolean b = redisTemplate.opsForZSet().add(key, member, score);
            return b;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据key和member获取分数
     *
     * @param key
     * @param member
     * @return
     */
    public double scoreZSet(Object key, Object member) {
        try {
            Double score = redisTemplate.opsForZSet().score(key, member);
            return score;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 根据key和member移除缓存
     *
     * @param key
     * @param member
     * @return
     */
    public long removeZSet(Object key, Object member) {
        try {
            Long l = redisTemplate.opsForZSet().remove(key, member);
            return l;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

}
